<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<jsp:directive.page import="db.DBHander"/>
<jsp:directive.page import="java.io.File"/>
 <%
	
	String userName = "";
	String role ="";
	if(session.getAttribute("userName")!=null &&session.getAttribute("role")!=null) {
		userName =(String) session.getAttribute("userName");
		role=  (String)session.getAttribute("role");
		
	}
	if(role.compareTo("admin")!=0){
		response.sendRedirect("login.jsp");
		//System.out.println("pageValidate.jsp not admin");	
	}
%>    
<%
	String type = request.getParameter("type");
	
	String id =	request.getParameter("id");
	String product_code =	request.getParameter("product_code");
	String name_cn =	request.getParameter("name_cn");
	String name_en =	request.getParameter("name_en");
	String description_cn =	request.getParameter("description_cn");
	String description_en =	request.getParameter("description_en");
	String retail_box =	request.getParameter("retail_box");
	String retail_big_box =	request.getParameter("retail_big_box");
	String pic_path =	request.getParameter("pic_path");
	String description =	request.getParameter("description");
	String gradation =	request.getParameter("gradation");
	String enable =	request.getParameter("enable");	
	String cp_class = request.getParameter("cp_class");
	
	//out.println("<br> product_code = " + product_code);
	String id1 = id;
	
	if(id==null && type==null){
		id1="";
	}	
	

%>    

<%
//--------------delete -------------

	if(id!=null && type != null && type.compareTo("delete")==0  && id.compareTo("1")!=0 ){
		//out.println("delete");
		
		DBHander db4 = new DBHander();
		String sql4 ="delete from products where id = "+id;
		db4.stmt.executeUpdate(sql4);
		db4.releaseAll();
		//response.sendRedirect("userAdmin.jsp");	
		
		
		DBHander db5 = new DBHander();
		String newId1 = null;
		String sql5 = "select * from products order by id desc";
		db5.rs = db5.stmt.executeQuery(sql5);
		if(	db5.rs.next()){
			newId1 = db5.rs.getString(1);		
		}
		
		db5.releaseAll();
		if(newId1 != null){
			id1 = newId1;
		}else{
			id1="";
		}		
		
		
		//-----------delete uploadFiles -----------------
		
	    String savePath = getServletContext().getRealPath("/");
	    
	    savePath +="uploadfiles\\"+ pic_path.substring(pic_path.lastIndexOf("/")+1);
	    //System.out.println("del -- file = " + savePath);	    
	    java.io.File f = new File(savePath);
	    if(f.exists()){
	    	f.delete();
	    }
	    
	}



    
    
    
    








%>




<%
//----------------update user-----------------
	if(type !=null && type.compareTo("update")==0 && id!=null){
		
	
	
		DBHander db3 = new DBHander();
		String sql3 = "update products set PRODUCT_CODE='"+product_code
		+"',  NAME_CN='"+name_cn
		+"',  NAME_EN='"+name_en
		+"',  DESCRIPTION_cn='"+description_cn
		+"',  DESCRIPTION_en='"+description_en
		+"',  RETAIL_BOX='"+retail_box
		+"',  RETAIL_BIG_BOX='"+retail_big_box
		+"',  PIC_PATH='"+pic_path
		+"',  DESCRIPTION='"+description
		+"',  GRADATION='"+gradation
		+"',  class1='"+ cp_class
		//+"',  ENABLE='"+ enable 
		+"'  where id ="+id;
		//System.out.println(sql3);
		db3.stmt.executeUpdate(sql3);		
		db3.releaseAll();
		id1= id;
		//response.sendRedirect("userAdmin.jsp?id="+id);
		
	}
%>









<%
//------------new ----------------

	
	String newId = null;

	if(type!=null && type.compareTo("new") == 0 ){
		DBHander db1 =null;
		DBHander db2 =null;
		
		db2 = new DBHander();	
		String sql2 = "insert into products () values()";
		db2.stmt.executeUpdate(sql2);
		db2.releaseAll();
		
		
		db1 = new DBHander();
		String sql1 = "select * from products order by id desc";
		db1.rs = db1.stmt.executeQuery(sql1);
		if(	db1.rs.next()){
			newId = db1.rs.getString(1);		
		}
		
		db1.releaseAll();
		if(newId != null){
			id1 = newId;
		}
		else{
			id1="1";
		}
	}
%>  

<%
	
	//response.sendRedirect("productAdmin.jsp?id="+id1);		
	%>
	<html>
		<head><link REL = "Shortcut Icon" href="http://127.0.0.1:8080/hx10/favicon.ico">
		<script type="text/javascript">
			function back(){
				var forms = document.getElementsByTagName('form');
				forms[0].submit();				
				//for(i=0;i<forms.length;i++){
				//	var formName = forms[i].name;
				//	alert(formName);
					//if(forms[i].id=='frm'){
					//	frm = forms[i];
					//}
				//}

			}
		</script>
		
		</head>
		<body onload='back()'>
			<form id='frm' action=productAdmin.jsp method=post>
				<input type=hidden value='<%=id1%>' name=id />			
			</form>
		</body>
	</html>
    
